package com.poetry.controller.common;

import com.poetry.domain.dto.PoemDoc;
import com.poetry.domain.po.Search;
import com.poetry.exception.R;
import com.poetry.exception.SearchResult;
import com.poetry.service.ISearchService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("/search")
@Tag(name = "搜索管理模块")
public class SearchController {
    @Resource
    private ISearchService searchService;
    @GetMapping("/search_poem")
    @Operation(summary = "搜索诗词")
    public R<SearchResult<PoemDoc>> searchPoem(@RequestParam String query,
                                               @RequestParam(defaultValue = "title") String type,
                                               @RequestParam(required = false) String dynasty,
                                               @RequestParam(required = false) String author,
                                               @RequestParam(defaultValue = "0") int page,
                                               @RequestParam(defaultValue = "10") int size) {
        return searchService.searchPoems(query, type, dynasty, author, page, size);
    }

    @GetMapping("/suggestion")
    @Operation(summary = "搜索自动补全")
    public R<List<String>> getSuggestions(@RequestParam String prefix,
                                          @RequestParam(defaultValue = "10") int size) {
        return searchService.getSuggestions(prefix, size);
    }

    @Operation(summary = "添加搜索记录")
    @PostMapping("/add_search_history")
    public R<String> addHistory(@RequestParam String content) {
        return searchService.addHistory(content);
    }

    @GetMapping("/get_search")
    @Operation(summary = "查询浏览记录")
    public R<List<Search>> queryHistory(@RequestParam(defaultValue = "10") Integer limit) {
        return searchService.queryHistory(limit);
    }

    @Operation(summary = "清空历史记录")
    @DeleteMapping("/clear_search")
    public R<String> clearHistory() {
        return searchService.clearHistory();
    }
}
